package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.List;
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    //创建代理类
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        //删除关系
        employeeMapper.deleteRelation(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        //对密码将进行加密(加盐,用户名作为盐employee.getName())
        String password = employee.getPassword();
        Md5Hash md5Hash = new Md5Hash(password, employee.getName());
        //设置加密的结果
        employee.setPassword(md5Hash.toString());


        employeeMapper.insert(employee);
        //判断ids是否为零,处理中间表单额关系
        if(ids!=null&&ids.length>0){
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(),rid);//第一个员工id,第二个关系id
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //因为我们创建了角色,然后在重新添加的相同的数据的时候会出现重复,所以要先删除
        employeeMapper.deleteRelation(employee.getId());
        //判断ids是否为零,处理中间表单额关系
        if(ids!=null&&ids.length>0){
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(),rid);//第一个员工id,第二个关系id
            }
        }
    }

    @Override
    public Employee login(String username, String password) {
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        //判断用户是否存在
        if (employee==null){
            //抛出异常
            throw new RuntimeException("账号和密码不存在");
        }
        return employee;
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<Employee> list = employeeMapper.selectForList(qo);
        //调用四个构造器
        return new PageInfo(list);



       /* //定义两个方法,第一个是查询总数
        int count = employeeMapper.selectForCount(qo);
        //判断是否有值
        if (count==0){
            return new PageResult(qo.getPageSize());
        }
        //创建第二个方法,查询结果集
        List<Employee> list = employeeMapper.selectForList(qo);
        //调用四个构造器
        return new PageResult(qo.getCurrentPage(),count,list,qo.getPageSize());*/
    }




    public void batchDelete(Long[] ids) {
        employeeMapper.deleteBatch(ids);
    }

    @Override
    public Employee selectByName(String name) {
        return employeeMapper.selectByName(name);
    }

    @Override
    public Workbook exportXls() {
        //创建文件
        Workbook wb = new HSSFWorkbook();
        //创建一页纸sheet
        Sheet sheet = wb.createSheet("员工名单");
        //标题行,起始位置为0
        Row row = sheet.createRow(0);
        //设置内容到单元格中
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        //查询员工数据
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0;i<= employees.size();i++) {
            //创建员工对象
            Employee employee = employees.get(i);
            //创建行(每个员工就是一行)
            row = sheet.createRow(i+1);
            //设置内容到单元格中
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            row.createCell(2).setCellValue(employee.getAge());
        }
        return wb;
    }

    @Override
    public void inportXls(MultipartFile file) throws IOException {
        //把接收到的文件以Excel的方式读取并保存
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //读取每一页
        Sheet sheetAt = wb.getSheetAt(0);
        //获取总行数
        int lastRowNum = sheetAt.getLastRowNum();
        //从索引为1的行数开始读(忽略标题行)
        for (int i = 1; i <= lastRowNum; i++) {
            //获取行数据
            Row row = sheetAt.getRow(i);
            //创建员工对象
            Employee employee = new Employee();
            //写入
            employee.setName(row.getCell(0).getStringCellValue());
            employee.setEmail(row.getCell(1).getStringCellValue());
            //如果Excel单元格设置的是文本格式的话用这个
           // employee.setAge(Integer.valueOf(row.getCell(2).getStringCellValue()));

            //如果Excel单元格设置的是数字格式的话用这个
            double value = row.getCell(2).getNumericCellValue();
            employee.setAge((int)value);

            //设置默认密码
            employee.setPassword("1");
            //调用保存的方法
            this.save(employee,null);
        }
    }

    @Override
    public List<Employee> selectByRoleSn(String... sns) {
        return employeeMapper.selectByRoleSn(sns);
    }
}


